package com.longxi.tianjintianbao.ui.adapter.multitype.work;

import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.longxi.tianjintianbao.AppConst;
import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.greedao.entity.Work;
import com.longxi.tianjintianbao.listener.AdapterListener;
import com.longxi.tianjintianbao.utils.MyUtils;

import me.drakeet.multitype.ItemViewBinder;
import q.rorbin.badgeview.Badge;
import q.rorbin.badgeview.QBadgeView;

/**
 * Created by cxw on 2018/2/20.
 */
public class WorkViewBinder extends ItemViewBinder<Work, WorkViewBinder.ViewHolder> {

    AdapterListener mListener;
    public WorkViewBinder(AdapterListener listener) {
        mListener = listener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View root = inflater.inflate(R.layout.item_worktable_work, parent, false);
        return new ViewHolder(root);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull Work work) {
        if(new Long(work.getId()).intValue() == 0){
            holder.mLlBtn.setBackgroundResource(R.color.colorPrimary);
        }else{
            holder.mIvImage.setImageResource(AppConst.mImage[new Long(work.getId()).intValue() - 1]);
            holder.mTvName.setText(MyUtils.getString(AppConst.mTitle[new Long(work.getId()).intValue() - 1]));
            holder.mLlBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mListener.onClick(holder.getAdapterPosition());
                }
            });
            if(work.getCategory() == 1){
                holder.mLlBtn.setBackgroundResource(R.color.colorPrimary);
                holder.mTvName.setTextColor(MyUtils.getResource().getColor(R.color.white));
            }else{
                holder.mLlBtn.setBackgroundResource(R.color.white);
                holder.mTvName.setTextColor(MyUtils.getResource().getColor(R.color.black));
            }
            holder.badge.setBadgeNumber(work.getMessage());
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        RelativeLayout mLlBtn;
        ImageView mIvImage;
        TextView mTvName;
        Badge badge;
        ViewHolder(View itemView) {
            super(itemView);
            mLlBtn = itemView.findViewById(R.id.llBtn);
            mIvImage = itemView.findViewById(R.id.ivImage);
            mTvName = itemView.findViewById(R.id.tvName);
            badge = new QBadgeView(mIvImage.getContext()).bindTarget(mIvImage);
            badge.setBadgeGravity(Gravity.TOP | Gravity.END);
            badge.stroke(Color.WHITE, 1, true);
            badge.setShowShadow(true);
            badge.setBadgePadding(6, true);

        }
    }
}
